package com.erpanderp.wam.adm.model.service;

import java.util.List;

import org.hibernate.FlushMode;
import org.hibernate.Query;
import org.hibernate.classic.Session;

import com.erpanderp.wam.adm.model.entity.CustomLookup;
import com.erpanderp.wam.adm.model.entity.LookupValue;
import com.erpanderp.wam.adm.model.entity.Resource;
import com.erpanderp.wam.util.HibernateUtil;

public class CustomLookupDAO {
	
	public CustomLookup createCustomLookup(CustomLookup customLookUp){
	try{
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		session.setFlushMode(FlushMode.AUTO);
		session.save(customLookUp);
		session.getTransaction().commit();
		return customLookUp;
	}catch (Exception e) {
		e.printStackTrace();
		return null;
		// TODO: handle exception
	}
	}
	public LookupValue createLookupValue(LookupValue lookUpValue){
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.AUTO);
			session.save(lookUpValue);
			session.getTransaction().commit();
			return lookUpValue;
		}catch (Exception e) {
			e.printStackTrace();
			return null;
			// TODO: handle exception
		}
		}
	
	
	public void createCustomLookupValues(List<LookupValue> lookUpValuesList){
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.AUTO);
			for(LookupValue lookupValue : lookUpValuesList){
			 session.save(lookupValue);
			}
			session.getTransaction().commit();
			
		}catch (Exception e) {
			e.printStackTrace();
				// TODO: handle exception
		}
		}
	
	
	
	public List<CustomLookup> getAllCustomLookups(){
		List<CustomLookup> customLookupsList = null;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.AUTO);
			String queryString = "from CustomLookup c";
			Query query = session.createQuery(queryString);
			customLookupsList = query.list();
			session.getTransaction().commit();
			return customLookupsList;
		}catch (Exception e) {
			e.printStackTrace();
			return null;
			// TODO: handle exception
		}
		}
	
	
	public List<LookupValue> getLookupValuesList(Long customLookId){
		List<LookupValue> lookupValuesList = null;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.AUTO);
			String queryString = "from LookupValue l where l.customLookup.customLookId =:customLookId";
			Query query = session.createQuery(queryString).setLong("customLookId", customLookId);
			lookupValuesList = query.list();
			session.getTransaction().commit();
			return lookupValuesList;
		}catch (Exception e) {
			e.printStackTrace();
			return null;
			// TODO: handle exception
		}
		}
	public List<LookupValue> getLookupValuesListByCustomLookupName(String customLookupName){
		List<LookupValue> lookupValuesList = null;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.AUTO);
			String queryString = "from LookupValue l where l.customLookup.customLookupName =:customLookupName";
			Query query = session.createQuery(queryString).setString("customLookupName", customLookupName);
			lookupValuesList = query.list();
			session.getTransaction().commit();
			return lookupValuesList;
		}catch (Exception e) {
			e.printStackTrace();
			return null;
			// TODO: handle exception
		}
		}
	
	
	
	public CustomLookup getCustomLookUp(String customLookupName){
		CustomLookup customLookup= null;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.AUTO);
			String queryStrng = "from CustomLookup c where c.customLookupName =:customLookupName";
			Query query = session.createQuery(queryStrng).setString("customLookupName", customLookupName);
			customLookup =(CustomLookup) query.uniqueResult();
			session.getTransaction().commit();
			return customLookup;
		}catch (Exception e) {
			e.printStackTrace();
			return null;
			// TODO: handle exception
		}
	}
	
	
	public CustomLookup getCustomLookUpById(Long customLookId){
		CustomLookup customLookup= null;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.AUTO);
			String queryStrng = "from CustomLookup c where c.customLookId =:customLookId";
			Query query = session.createQuery(queryStrng).setLong("customLookId", customLookId);
			customLookup =(CustomLookup) query.uniqueResult();
			session.getTransaction().commit();
			return customLookup;
		}catch (Exception e) {
			e.printStackTrace();
			return null;
			// TODO: handle exception
		}
	}
	
	public boolean isAvailableCustomLookup(String customLookupName,String displayName){
		System.out.println("CUSTOMLOOKUP DUPLICATE ENTRY CHECK POINT"+customLookupName+" "+displayName);
		CustomLookup customLookup = null;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.AUTO);
			String queryStrng = "from CustomLookup c where c.customLookupName =:customLookupName or c.displayName=:displayName";
			Query query = session.createQuery(queryStrng).setString("customLookupName", customLookupName).setString("displayName", displayName);
			customLookup =(CustomLookup) query.uniqueResult();
			if(customLookup != null){
			 return true;
			}else{
				return false;
			}
		}catch (Exception e) {
			e.printStackTrace();
			return false;
			// TODO: handle exception
		}
	}
	
	
	public boolean updateCustomLookup(CustomLookup customLookup){
		boolean isCustomLookupUpdated = false;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.AUTO);
			session.update(customLookup);
			session.getTransaction().commit();
			isCustomLookupUpdated = true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
		return isCustomLookupUpdated;
		
	}

}
